'''
LastEditors: lf
Date: 2020-12-27 23:44:32
LastEditTime: 2020-12-28 00:28:19
FilePath: \022爬取图片\tieba.py
'''
import re
import urllib.request

'''
description: 打开url
param {*}url
return {*}html
'''
def openUrl(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
    page = urllib.request.urlopen(req)
    html = page.read().decode('utf-8')
    return html

'''
description: 下载图片
param {*}
return {*}
'''


'''
description: 获取图片
param {*}html
return {*}
'''
def getImg(html):
    picture_url = r'<img class="BDE_Image" src="([^"]+\.jpg)"'
    picture_list = re.findall(picture_url, html)
    for each in picture_list:
        print("下载地址为： ", each)
        filename = each.split("/")[-1]
        urllib.request.urlretrieve(each, filename, None)


'''
description: 主函数
param {*}
return {*}
'''
if __name__ == "__main__":
     url = "https://tieba.baidu.com/p/6103635019"
     getImg(openUrl(url))
     